import java.util.HashMap;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-11-02
 * Time:10:04
 */

class HashBuck{
    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key,int val){
            this.key = key;
            this.val = val;
        }
    }

    public Node[] array;
    public int usedSize;//存放了几个元素
    public HashBuck(){
        this.array = new Node[8];
    }
    //通过KEY值获取value值
    public int get(int key){
        int index = key % array.length;

        Node cur = array[index];
        while (cur!=null){
            if (cur.key == key){
                return cur.val;
            }
            cur = cur.next;
        }
        return 0;//找不到的情况
    }


    public  void put(int key,int val){
        Node newNode = new Node(key,val);
        //找下标
        int index = key % array.length;

        Node cur = array[index];

        while (cur!=null){
            if (cur.key == key){
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        cur = array[index];
        if (cur == null){
            array[index] = newNode;
        }else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = newNode;
        }
        this.usedSize++;

        if (loadFactor() >= 0.75){
            resize();

        }
    }

    private void resize() {
        Node[] newArray = new Node[array.length * 2];
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while (cur != null){
                Node curnext = cur.next;
                //计算当前key在新数组中的下标
                int index = cur.key % newArray.length;

                Node curN = newArray[index];
                if (curN == null){
                    newArray[index] = cur;
                }else {
                    while (curN.next != null) {
                        curN = curN.next;
                    }
                    curN.next = cur;

                }
                cur.next = null;
                cur = curnext;
            }
        }
        array = newArray;
    }

    public double loadFactor(){
        return this.usedSize * 1.0 / array.length;
    }

}
public class TestDemo {
    public static void main(String[] args) {
        HashMap<String,String> hashMap = new HashMap<>();
    }
    public static void main1(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1,11);
        hashBuck.put(2,22);
        hashBuck.put(3,33);
        hashBuck.put(17,177);
        hashBuck.put(24,244);
        hashBuck.put(15,155);
        hashBuck.put(29,299);
        hashBuck.put(19,299);
        System.out.println("0000");
    }
}
